﻿Module Module13

    Sub Main()
        'Call Test1()
        'Call Test2()
        'Call Test3()
        Call Test4()
    End Sub

    Sub Test1()
        Console.WriteLine("Module13 Test1()")
        Dim A(5) As Integer
        For i = 0 To 5
            Console.WriteLine("A({0})={1}", i, A(i))
        Next
        Console.ReadKey(True) ' 按鍵不顯示
    End Sub

    Sub Test2()
        Console.WriteLine("Module13 Test2()")
        Dim A() As Integer
        A = New Integer() {18, 7, 25, 40, 38, 21}
        For Each x As Integer In A
            Console.WriteLine(x)
        Next
        Console.ReadKey(True) ' 按鍵不顯示
    End Sub

    Sub Test3()
        Console.WriteLine("Module13 Test3()")
        Dim A() As Integer
        Dim B() As Integer

        A = New Integer(5) {}
        B = A
        Console.WriteLine("A(0) = " & A(0))
        B(0) = 100
        Console.WriteLine("A(0) = " & A(0))

        Call WriteArray(A)
        Call WriteArray(B)

        Console.ReadKey(True) ' 按鍵不顯示
    End Sub

    Sub WriteArray(ByVal A() As Integer)
        For Each x As Integer In A
            Console.Write(x & vbTab)
        Next
        Console.WriteLine()
    End Sub

    Sub Test4()
        Console.WriteLine("Module13 Test4()")

        Dim A() As Integer = {18, 7, 25, 40, 38, 21}
        WriteArray(A)

        ' 排序
        Array.Sort(A)
        WriteArray(A)

        ' 反轉
        Array.Reverse(A)
        WriteArray(A)

        ' 搜尋
        Dim index As Integer
        index = Array.IndexOf(A, 25)
        Console.WriteLine("IndexOf(A,25) = " & index)

        ' 二分搜尋前必須已經過排序
        index = Array.BinarySearch(A, 25)
        Console.WriteLine("BinarySearch(A,25) = " & index)

        ' 從索引3開始連續清除2個項目
        Array.Clear(A, 3, 2)
        WriteArray(A)


        Console.ReadKey(True) ' 按鍵不顯示
    End Sub

End Module
